package com.shujuku.service;

import com.shujuku.test2.MyTestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.List;
import java.util.Map;

/**
 * 作者: LCG
 * 日期: 2020/3/19 22:51
 * 描述:
 */
@Component
public class MyTestService {

    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    private DefaultTransactionDefinition defaultTransactionDefinition;

    @Autowired
    private MyTestMapper myTestMapper;

    //@Transactional(propagation = Propagation.REQUIRED)
    public void test01(String id1,String id2) throws InterruptedException {
        TransactionStatus transaction = dataSourceTransactionManager.getTransaction(defaultTransactionDefinition);
        List<Map<String, String>> data1 = myTestMapper.getData1(id1);
        System.out.println("===1====>"+data1);
        Thread.sleep(5000);
        List<Map<String, String>> data2 = myTestMapper.getData2(id2);
        System.out.println("===1====>"+data2);
        dataSourceTransactionManager.commit(transaction);
    }

    //@Transactional(propagation = Propagation.REQUIRED)
    public void test02(String id1,String id2) throws InterruptedException {
        TransactionStatus transaction = dataSourceTransactionManager.getTransaction(defaultTransactionDefinition);
        List<Map<String, String>> data2 = myTestMapper.getData2(id2);
        System.out.println("===2====>"+data2);
        Thread.sleep(5000);
        List<Map<String, String>> data1 = myTestMapper.getData1(id1);
        System.out.println("===2====>"+data1);
        dataSourceTransactionManager.commit(transaction);
    }

}
